package com.jml.表单重复提交;

import com.sun.xml.internal.ws.util.StringUtils;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/DoFormServlet")
public class DoFormServlet extends HttpServlet {

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		boolean flag = isFlag(req);
		if(!flag){
			resp.getWriter().write("该表单已经重复提交");
			return;
		}
		String userName = req.getParameter("userName");

		try {
			Thread.sleep(300);
		} catch (InterruptedException e) {
		}
		System.out.println("userName:"+userName);
		resp.getWriter().write("success");
	}

	public boolean isFlag(HttpServletRequest req){
		String sessionToken = (String)req.getSession().getAttribute("sessionToken");
		if(sessionToken==null||"".equals(sessionToken)){
			System.out.println("不要重复提交！");
			return false;
		}
		String sessionTokenTable = req.getParameter("sessionToken");
		if(sessionToken.equals(sessionTokenTable)){
			req.getSession().removeAttribute("sessionToken");
			return true;
		}else{
			System.out.println("不要重复提交！");
			return false;
		}
	}
}
